/// 人才发现用户模型
/// 用于人才页面展示用户信息

class TalentUserModel {
  /// 用户ID
  final int id;
  
  /// 用户昵称
  final String nickname;
  
  /// 用户头像
  final String? avatar;
  
  /// 用户标签/技能列表
  final List<String> tags;
  
  /// 与当前用户的距离（米）
  final double? distance;
  
  /// 是否是热门用户
  final bool isHot;
  
  /// 用户简介
  final String? bio;
  
  /// 用户位置
  final String? location;
  
  /// 评分
  final double? rating;
  
  /// 评价数量
  final int? reviewCount;
  
  /// 粉丝数
  final int? followersCount;
  
  /// 是否在线
  final bool isOnline;

  TalentUserModel({
    required this.id,
    required this.nickname,
    this.avatar,
    this.tags = const [],
    this.distance,
    this.isHot = false,
    this.bio,
    this.location,
    this.rating,
    this.reviewCount,
    this.followersCount,
    this.isOnline = false,
  });

  /// 从JSON创建模型
  factory TalentUserModel.fromJson(Map<String, dynamic> json) {
    return TalentUserModel(
      id: json['id'] ?? 0,
      nickname: json['nickname'] ?? json['username'] ?? '',
      avatar: json['avatar'],
      tags: (json['tags'] as List<dynamic>?)
              ?.map((e) => e.toString())
              .toList() ??
          [],
      distance: (json['distance'] as num?)?.toDouble(),
      isHot: json['isHot'] ?? json['is_hot'] ?? false,
      bio: json['bio'], // 后端暂不返回，保留字段
      location: json['location'], // 后端暂不返回，保留字段
      rating: json['rating']?.toDouble(), // 后端暂不返回，保留字段
      reviewCount: json['reviewCount'] ?? json['review_count'], // 后端暂不返回，保留字段
      followersCount: json['followersCount'] ?? json['followers_count'], // 后端暂不返回，保留字段
      isOnline: json['isOnline'] ?? json['is_online'] ?? false, // 后端暂不返回，保留字段
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'nickname': nickname,
      'avatar': avatar,
      'tags': tags,
      'distance': distance,
      'isHot': isHot,
      'bio': bio,
      'location': location,
      'rating': rating,
      'reviewCount': reviewCount,
      'followersCount': followersCount,
      'isOnline': isOnline,
    };
  }

  /// 获取格式化的距离文本
  String get formattedDistance {
    if (distance == null) return '';
    if (distance! < 1000) {
      return '${distance!.toInt()}m';
    } else {
      return '${(distance! / 1000).toStringAsFixed(1)}km';
    }
  }

  /// 获取格式化的评分
  String get formattedRating {
    if (rating == null) return '暂无评分';
    return rating!.toStringAsFixed(1);
  }
}

